package com.suneast.suneaststore.modules.store.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.suneast.suneaststore.R;
import com.suneast.suneaststore.bean.homepage.StoreListBean;
import com.suneast.suneaststore.ui.customview.RoundImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zestz on 2018/3/7.
 */

public class StoreListAdapter extends RecyclerView.Adapter<StoreListAdapter.ViewHolder> implements View.OnClickListener {
    private List<StoreListBean.Store> list = new ArrayList<>();

    private OnClickListener listener = null;

    private final static String BUSINESS_HEADER = "主营: ";
    private StringBuilder builder = new StringBuilder(BUSINESS_HEADER);
    @Override
    public void onClick(View v) {
        if (listener != null) {
            listener.onItem((Integer) v.getTag());
        }
    }

    public interface OnClickListener {
        void onItem(int position);
        void onNavigate(int position);
    }

    public void setList(List<StoreListBean.Store> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view  = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_exploration_hall, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        StoreListBean.Store store = list.get(position);

        Glide.with(holder.ivHall.getContext()).load(store.getLogo()).into(holder.ivHall);
        holder.tvHallContent[0].setText(store.getMerchname());
        List<String> business = store.getBusiness();
        builder.delete(BUSINESS_HEADER.length(), builder.length());
        for (int i = 0, len = business.size(); i < len; i++) {
            builder.append(business.get(i));
            if (i != len - 1) {
                builder.append(" ");
            }
        }
        holder.tvHallContent[1].setText(builder.toString());
        holder.tvHallContent[2].setText(store.getProvince()+" "+store.getCity()+" "+store.getArea());
        holder.tvHallContent[3].setText(store.getDistance() >= 1000 ? (store.getDistance()/1000+"km") : store.getDistance()+"m");
        holder.tvHallContent[4].setOnClickListener(v -> {
            if (listener != null) {
                listener.onNavigate(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public void setOnclickListener(OnClickListener listener) {
        this.listener = listener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private RoundImageView ivHall;
        private TextView []tvHallContent = new TextView[6];
        ViewHolder(View itemView) {
            super(itemView);
            ivHall = itemView.findViewById(R.id.iv_hall);
            tvHallContent[0] = itemView.findViewById(R.id.tv_hall_name);
            tvHallContent[1] = itemView.findViewById(R.id.tv_hall_business);
            tvHallContent[2] = itemView.findViewById(R.id.tv_hall_location);
            tvHallContent[3] = itemView.findViewById(R.id.tv_hall_distance);
            tvHallContent[4] = itemView.findViewById(R.id.tv_start_navigate);
        }
    }
}
